class UsersController < ApplicationController
  before_filter :require_no_user, :only => [:new, :create]
  before_filter :require_user, :except => [:new, :create, :feed, :about]
  
  layout 'users', :except => ['feed']

  def index
    #@logged_in_count = User.logged_in.count
    @admins_logged_in = User.logged_in.find(:all, :conditions => {:group_id => 1}).length
    @count_unread = Message.find(:all, :conditions => ['read = \'f\' and recipient = ?', current_user.id ]).length
  end
  
  def new
    @user = User.new
  end

  # tworzenie nowego uzytkownika, automatyczne dodanie pierwszego uzytkownika
  # do grupy administratorow
  def create
    @user = User.new(params[:user])
    # sprawdzenie, czy w bazie istnieje tylko jeden uzytkownik,
    # jesli tak, wtedy wpisujemy go do grupy Administratorzy
    if User.find(:all).length == 0
      # przypisanie do grupy "Administratorzy"
      @user.group_id = 1
      @notice = "Witaj nowy administratorze!"
      @user.active = true
      #controller = "admin"
       if @user.save
        # ustawienie wiadomosci po zalogowaniu
        flash[:notice] = @notice
        # przekierowanie do wybranego kontrolera
        redirect_to :controller => 'admin'
      else
        render :action => 'new'
      end
    else
      # przypisanie do grupy "Użytkownicy"
      @user.group_id = 2
      if @user.save#_without_session_maintenance
        @user.deliver_activation_instructions!
        flash[:notice] = "Rejestracja przeprowadzona pomyślnie. Teraz należy kliknąć link aktywujący konto na Twoim e-mailu!"
        redirect_to :controller => "users", :action => "about"
      else
        render :action => 'new'
      end
    end
  end

  def show_user
    @user = current_user
  end

  def edit
    @user = current_user
  end

  def update
    @user = current_user # makes our views "cleaner" and more consistent
    if @user.update_attributes(params[:user])
      flash[:notice] = "Dane zostały zaktualizowane!"
      redirect_to :controller => "users", :action => "edit"
    else
      flash[:error] = "Nieprawidłowe dane"
      render :action => 'edit'
    end      
  end  

  def payments
    @payments = current_user.payments.paginate :page => params[:page], :per_page => 5, :order => 'created_at DESC'
  end

  def prints
    @waiting_prints = current_user.prints.paginate_by_printed false, :page => params[:page], :per_page => 5, :order => 'created_at ASC'
    @printed_prints = current_user.prints.paginate_by_printed true, :page => params[:page], :per_page => 5, :order => 'updated_at DESC'
  end

  def contact
    @user = current_user
    @message = Message.new
    @messages_sent = Message.find(:all, :conditions => "sender=" + current_user.id.to_s )
    @messages_received = Message.find(:all, :conditions => "recipient=" + current_user.id.to_s, :order => 'read')
  end

  def download
    @print = Print.find(params[:id])
    if (current_user.id == @print.user_id) || (current_user.group.may_view)
      ct = @print.attachment_content_type
      headers['Content-Type'] = ct
      send_file "uploaded_files/" + @print.id.to_s + "/" + @print.attachment_file_name, :type => ct
    else
      flash[:error] = 'Nie masz dostępu do tego pliku :> Nie hakeruj...'
      redirect_to :action => 'index'
    end
  end

  def feed    
    @waiting_prints = Print.find_all_to_print
    respond_to do |format|
     format.atom
    end
  end 

  def send_reminder
    @user = User.find(params[:id])
    @text = "Uprzejmie prosimy o uregulowanie kwoty! W tej chwili zalegasz około " + @user.saldo.round.abs.to_s + " zł."
    @message = Message.new
    @message.title = 'Prośba o uregulowanie należności'
    @message.body = @text
    Print.change_bw_print_price(0.2)
    @message.sender = User.find(:all, :conditions => "GROUP_ID = 1" ).first
    @message.recipient = @user.id
    if @message.save!
      redirect_to :controller => 'admin', :action => 'payments'
      flash[:notice] = "Upomnienie zostało wysłane!"
    end
  end
  protected

end
